{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "map函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import sys\n",
    "import re\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    handler = sys.stdin    #接收管道输出作为输入\n",
    "    for line in handler:\n",
    "        if not line:\n",
    "            continue\n",
    "        terms = line.strip().split(\" \")   #strip()用于去除每一行换行 split（）用于把每一行以空格拆分成单一元素\n",
    "        for i in terms:    #把拆分好的对象遍历输出\n",
    "            print(i)\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "reduce 函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import sys\n",
    "import re\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    handler = sys.stdin  \n",
    "    word_dict = {}\n",
    "    for line in handler:\n",
    "        if not line:\n",
    "            continue\n",
    "        terms = line.strip().split(\" \")\n",
    "        for i in terms:\n",
    "            if i in word_dict:\n",
    "                word_dict[i] += 1  #如果i在字典中，值就累加1\n",
    "            else:\n",
    "                word_dict[i] = 1   #i不在字典中，就它自己，所以赋值1\n",
    "\n",
    "    for j in word_dict:            #遍历输出\n",
    "        print(j, word_dict[j])\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "分布式集群依据mapreduce原理。map函数负责数据收集，reduce负责数据归纳，大概是这个意思吧！！这种思维源于庞大数据单一服务器无法处理或是处理缓慢无法满足及时的需求。好比秋天收稻谷，一大片稻谷要一个农民去收进粮仓，而且有时间限制，要求一天或者两天时间要全部收完，因为三天以后会有连续的大暴雨。很明显，工作任务量庞大，一个人无法完成，这个时候，这个人就会请20、30或者是50个小工来帮忙收稻谷，装进袋子里。这个人负责把稻谷袋子一袋袋往粮仓里搬运。这个人请来的这一群小工就相当于map函数所干的活，而这个人他本身就相当于扮演reduce的角色。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
